<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link href="../static/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/components.min.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/login.css" rel="stylesheet" type="text/css"/>
    <script src="../static/js/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">用户登录</h3>
        <div class="form-group">
            <label class="control-label">手机号</label>
            <div>
                <input type="text" class="form-control" placeholder="手机号" name="telephone" id="telephone">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">密码</label>
            <div>
                <input type="password" class="form-control" placeholder="密码" name="password" id="password">
            </div>
        </div>
        <div class="form-actions">
            <button id="login" class="btn blue" type="submit">登录</button>
            <button id="register" class="btn green" type="submit">注册</button>
        </div>
    </div>
</body>

<script>
    // DOM加载完毕后再执行
    jQuery(document).ready(function(){
        // 绑定OTP的click点击事件，向后端发送请求
        $("#login").on("click",function(){
            var telephone = $("#telephone").val();
            var password = $("#password").val();
            if (telephone == null || telephone == "") {
                alert("手机号不能为空");
                return false;
            }
            if (password == null || password == "") {
                alert("密码不能为空");
                return false;
            }
            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                xhrFields: {
                    "withCredentials": true
                },
                url: "http://localhost:8888/login",
                data: {
                    "telephone": telephone,
                    "password": password
                },
                success: (res)=>{
                    if (res.status == "success") {
                        alert("登录成功");
                        window.location.href="file:///E:/WorkSpace/Seckill/UI/templates/listItem.html";
                    } else {
                        alert("登录失败，原因为：" + res.data.errorMessage)
                    }
                },
                error: (res)=>{
                    alert("登录错误，原因为：" + res.responseText)
                }
            })
        });

        $("#register").on("click",function(){
            window.location.href="file:///E:/WorkSpace/Seckill/UI/templates/register.html";
        });

    });
</script>

</html>